﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ANZ_AIM_DAL;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using System.Net;



namespace ANZ_AIM_BLL
{
    public class LogBLL
    {

        private const string TABLE_NAME = "Log";
        private string connectionString;
        private string description;
        private Exception ex;

        public string Description
        {
            get { return description; }
            set { description = value; }
        }


        public Exception Ex
        {
            get { return ex; }
            set { ex = value; }
        }

        public LogBLL()
        {
            connectionString = DBHelper.GetLogConnectionString();
        }

        public void CheckConnection()
        {
            using (var con = new OleDbConnection())
            {
                con.ConnectionString = connectionString;
                try
                {
                    con.Open();
                }
                catch (Exception ex) { throw; }
            }
        }

        public string GetIpAddress()
        {
            string str_Ip = "";
            try
            {
                string hostName = Dns.GetHostName();
                str_Ip = Dns.GetHostByName(hostName).AddressList[0].ToString();

            }
            catch (Exception)
            {
                str_Ip = "No internet connection";

            }
            return str_Ip;
        }

        public void Log(Form frm, string username)
        {
            StackFrame frame = new StackFrame(1);
            var method = frame.GetMethod();
            string strEvent = method.Name;


            description = description == null || description == string.Empty ? this.ex != null ? "Message: " + ex.Message + ex.StackTrace : "Done: No error" : description;

            OleDbParameter[] cmdParams = new OleDbParameter[6];
            cmdParams[0] = new OleDbParameter("@IpAdd", GetIpAddress());
            cmdParams[1] = new OleDbParameter("@event", strEvent);
            cmdParams[2] = new OleDbParameter("@user", username == null || username == string.Empty ? "Null" : username);
            cmdParams[3] = new OleDbParameter("@form", frm.Name + " - [" + frm.Text + "]");
            cmdParams[4] = new OleDbParameter("@time", DateTime.Parse(DateTime.Now.ToLongTimeString()));
            cmdParams[5] = new OleDbParameter("@des", description);

            string command = string.Format("Insert into [{0}]([IpAddress], [Event], [User], [Form], [CreatedDate], [Description]) values(@IpAdd, @event, @user, @form, @time, @des)", TABLE_NAME);
            try
            {
                OleDbHelper.ExecuteNonQuery(this.connectionString, CommandType.Text, command, cmdParams);
            }
            catch (OleDbException oleEx)
            {
                throw oleEx;
            }
        }

        public DataTable LogFilter(string username, string fromDate, string toDate)
        {
            string command = "";
            const string DEFAULT_DATE = "01/01/0001";

            if (fromDate == DEFAULT_DATE && toDate == DEFAULT_DATE)
            {
                command = string.Format("Select * From [{0}] Where [User] Like '%{1}%' ORDER BY [ID] DESC", TABLE_NAME, username);
            }
            else if (fromDate != DEFAULT_DATE && toDate == DEFAULT_DATE)
            {
                command = string.Format("Select * From [{0}] Where [User] Like '%{1}%'and [CreatedDate] >= #{2} 00:00:00 AM# ORDER BY [ID] DESC", TABLE_NAME, username, fromDate);
            }
            else if (fromDate == DEFAULT_DATE && toDate != DEFAULT_DATE)
            {
                command = string.Format("Select * From [{0}] Where [User] Like '%{1}%'and [CreatedDate] <= #{2} 11:59:59 PM# ORDER BY [ID] ASC", TABLE_NAME, username, toDate);
            }
            else
            {
                command = string.Format("Select * From [{0}] Where [User] Like '%{1}%'and [CreatedDate] >= #{2} 00:00:00 AM# And [CreatedDate] <= #{3} 11:59:59 PM# ORDER BY [ID] DESC", TABLE_NAME, username, fromDate, toDate);
            }

            DataSet ds = OleDbHelper.ExecuteDataset(this.connectionString, CommandType.Text, command);
            return ds.Tables[0];
        }

        public class LogDescriptionPattern
        {
            public static class ForLogin
            {
                //Description pattern for Login Form
                public const string EMPTY_USERNAME = "Username is empty";
                public const string EMPTY_PASSWORD = "Password is empty";
                public const string LOGIN_OK = "Logged in successfully";
                public const string USER_INACTIVE = "User was inactive";
                public const string USER_NOT_FOUND = "User not found";
                public const string USER_LOCKED = "User was locked";
                public const string WRONG_PASS = "Wrong password";

            }
            //Description pattern for ChangePass Form
            public static class ForChangePass
            {
                public const string EMPTY_CURRENT_PASS = "Current password is empty";
                public const string EMPTY_NEW_PASS = "New password is empty";
                public const string EMPTY_CONFIRM_PASS = "Confirm password is empty";
                public const string CONFIRM_PASS_NOT_MATCH = "New Password and Confirm Password do not matched";
                public const string NEW_PASS_NOT_DIFFERENT_CURRENT = "New passwors is not different from current password";
                public const string NEW_PASS_NOT_DIFFERENT_3_PREVIOUS_PASS = "New passwors is not different from 3 previous passwords";
                public const string WRONG_CURRENT_PASS = "Current password is wrong";
                public const string USER_NOT_FOUND = "User is not found";
                public const string CHANGED_PASS_OK = "New password is changed";
                public const string NOT_A_STRONG_PASS = "This a not a strong password";

            }
            //Description pattern for CandidateOnffile Form
            public class ForCandidateOffline
            {
                public const string CANDIDATE_ADDED = "Added a candidate to database";
                public const string CANDIDATE_EDITED = "Saved candidate information to database";
                public const string EMPTY_PERSONAL_CODE = "Personal code is empty";
                public const string EMPTY_FULLNAME_E = "Fullname E is empty";
                public const string EMPTY_FULLNAME_V = "Fullname V is empty";
                public const string EMPTY_CELLPHONE = "Cellphone number is empty";
                public const string EMPTY_DATE_OF_BIRTH = "Date of birth is empty";
                public const string EMPTY_QUALIFICATION = "Qualification is empty";
                public const string EMPTY_COLLEGE = "College is empty";
                public const string EMPTY_ID_NUMBER = "Id number is empty";
                public const string EMPTY_ISSUED_DATE = "Issued date is empty";
                public const string EMPTY_PERMANENT_ADDRESS = "Permanent address is empty";
                public const string EMPTY_INTERVIEWER = "Interviewer is empty";
                public const string EMPTY_INTERVIEW_DATE = "Interview date is empty";
                public const string EMPTY_TRAINING_CALL_STATUS = "Traning call status is empty";
                public const string EMPTY_CIC_SACTION_DELIVERY_DATE = "Saction delivery date is empty";
                public const string EMPTY_CIC_SACTION_DELIVERY_NAME = "Saction delivery name is empty";
                public const string EMPTY_PEC_REMARK = "PEC remark is empty";
                public const string EMPTY_TRAINING_DATE = "Training date is empty";
                public const string EMPTY_TRAINING_BATCH = "Training batch is empty";
                public const string EMPTY_TRAINER_NAME = "Trainer name is empty";
                public const string EMPTY_TRAINING_ATTENDENCE_REMARK = "Training attendence remark is empty";
                public const string EMPTY_RETEST_REQUIREMENT = "Re-test requirement is empty";
                public const string EMPTY_TRAINING_RESULT = "Training result is empty";
            }
            //Description pattern for AdminUser Form
            public class ForAdminUser
            {
                public const string USER_ADDED = "User is added";
                public const string USER_SAVED = "User information is updated";
                public const string EMPTY_USERNAME = "Username is empty";
                public const string IN_USE_USERNAME = "Username is already in use";
                public const string EMPTY_FULLNAME = "Fullname is empty";
                public const string EMPTY_EMAIL = "Email is empty";
                public const string INVALID_EMAIL = "Email is invalid";
                public const string EMPTY_ROLE = "Role is empty";
            }
            //Description pattern for CandidateMaster Form
            public class ForCandidateMaster
            {
                public const string EMPLOYEE_IMPORTED_OK = "Employees are imported successfully";
                public const string CHANGED_INFORMATION = "Employee information is updated";
                public const string EDITED_CANDIDATE = "Candidate was updated succesfully";
                public const string DELETED_CANDIDATE = "A candidate was deleted";
                public const string ADDED_CANDIDATE = "A candidate was added";
                public const string MAIL_SENT = "Email was sent successfully";
            }

        }
    }
}
